package state.group.events;

import state.Globals;
import state.person.events.DestinationEvent;
import state.person.events.GoingInEvent;
import state.person.events.GoingOutEvent;
import state.person.events.InsideEvent;
import state.person.events.PersonRegisterEvent;
import state.person.events.WaitingEvent;

public class EventFactory {
    
    public static EventCollection parseEvents(byte[] data, int startIndex)
    {
        EventCollection retVal = new EventCollection();
        
        while (startIndex < data.length - 1)
        {
            int temp = parseSingleEvent(retVal, data, startIndex);
            if (temp == -1)
            {
                break;
            }
                startIndex += temp; 
        }
        
        return retVal;
        
    }
    
    private static int parseSingleEvent(EventCollection collToUpdate, byte[] data, int startIndex)
    {
        EventType type = EventType.getEventTypeForId(data[startIndex]); 
        BaseEvent event = null;
        switch (type)
        {
            case DoorEvent:
                event = new DoorEvent(data, startIndex); break;
            case FloorEvent:
                event = new FloorEvent(data, startIndex); break;
            case StartEvent:
                event = new StartEvent(data, startIndex); break;
            case StopEvent:
                event = new StopEvent(data, startIndex); break;
            case RegistrationEvent:
                event = new RegistrationEvent(data, startIndex); break;
            case DeregistrationEvent:
                event = new DeregistrationEvent(data, startIndex); break;
            case HallCallEvent:
                event = new HallCallEvent(data, startIndex); break;
            case PersonDispatcherEvent:
                event = new PersonDispatcherEvent(data, startIndex); break;
            case DirectionEvent:
            	event = new DirectionEvent(data, startIndex); break;
            case DestinationEvent:
                event = new DestinationEvent(data, startIndex); break;
            case GoingInEvent:
                event = new GoingInEvent(data, startIndex); break;
            case GoingOutEvent:
                event = new GoingOutEvent(data, startIndex); break;
            case InsideEvent:
                event = new InsideEvent(data, startIndex); break;
            case WaitingEvent:
                event = new WaitingEvent(data, startIndex); break;
            case PersonRegisterEvent:
                event = new PersonRegisterEvent(data, startIndex); break;
            default:
            	Globals.getLogger().log("Failed Message : " + type);
        }
        
        if (event != null){
        	collToUpdate.add(event);
        }
        
        return (event == null) ? -1 : event.getEventByteLength();
    }
    
}
